/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tomasio.projects.lua.core;

import com.tomasio.projects.lua.api.MainService;
import com.tomasio.projects.lua.core.exception.CoreException;
import com.tomasio.projects.lua.dao.ApplicationDAO;
import com.tomasio.projects.lua.dao.DAOFactory;
import com.tomasio.projects.lua.dao.GroupDAO;
import com.tomasio.projects.lua.dao.UserDAO;
import com.tomasio.projects.lua.exception.DAOException;
import com.tomasio.projects.lua.model.Application;
import com.tomasio.projects.lua.model.Group;
import com.tomasio.projects.lua.model.User;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author fernandofot
 */
public class MainServiceSimpleImpl implements MainService {

    private DAOFactory factory;

    public void setFactory(DAOFactory factory) {
        this.factory = factory;
    }

    public DAOFactory getFactory() {
        return factory;
    }

    @Override
    public String createApplication(Application application) {
        ApplicationDAO dao = factory.getApplicationDAO();
        try {
            return dao.create(application);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }
    }

    @Override
    public void updateApplication(Application application) {
        ApplicationDAO dao = factory.getApplicationDAO();
        try {
            dao.update(application);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }
    }

    @Override
    public void removeApplication(String applicationUID) {
        ApplicationDAO dao = factory.getApplicationDAO();
        try {
            dao.remove(applicationUID);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }
    }

    @Override
    public List<Application> searchApplications(String term) {
        ApplicationDAO dao = factory.getApplicationDAO();
        try {
            return dao.search(term);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }

    }

    @Override
    public List<Application> listApplications() {
        ApplicationDAO dao = factory.getApplicationDAO();
        try {
            return dao.findAll();
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }

    }

    @Override
    public Application findApplication(String applicationUID) {
        ApplicationDAO dao = factory.getApplicationDAO();
        try {
            return dao.find(applicationUID);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }
    }

    @Override
    public String createGroup(Group group) {
        GroupDAO dao = factory.getGroupDAO();
        try {
            return dao.create(group);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }
    }

    @Override
    public void updateGroup(Group group) {
        GroupDAO dao = factory.getGroupDAO();
        try {
            dao.update(group);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }
    }

    @Override
    public void removeGroup(String groupUID) {
        GroupDAO dao = factory.getGroupDAO();
        try {
            dao.remove(groupUID);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }
    }

    @Override
    public List<Group> searchGroup(String term) {
        GroupDAO dao = factory.getGroupDAO();
        try {
            return dao.search(term);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }
    }

    @Override
    public List<Group> listGroups() {
        GroupDAO dao = factory.getGroupDAO();
        try {
            return dao.findAll();
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }
    }
    
     @Override
    public List<Group> listGroups(String applicationUID) {
        GroupDAO dao = factory.getGroupDAO();
        try {
            return dao.findAll(applicationUID);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }
    }

    @Override
    public Group findGroup(String groupUID) {
        GroupDAO dao = factory.getGroupDAO();
        try {
            return dao.find(groupUID);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }
    }

    @Override
    public String createUser(User user) {
        UserDAO dao = factory.getUserDAO();
        try {
            return dao.create(user);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }
    }

    @Override
    public void updateUser(User user) {
        UserDAO dao = factory.getUserDAO();
        try {
            dao.update(user);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }
    }

    @Override
    public void removeUser(String userUID) {
        UserDAO dao = factory.getUserDAO();
        try {
            dao.remove(userUID);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }
    }

    @Override
    public List<User> searchUsers(String term) {
        UserDAO dao = factory.getUserDAO();
        try {
            return dao.search(term);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }
    }

    @Override
    public List<User> listUsers() {
        UserDAO dao = factory.getUserDAO();
        try {
            return dao.findAll();
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }
    }

    @Override
    public User findUser(String userUID) {
        UserDAO dao = factory.getUserDAO();
        try {
            return dao.find(userUID);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        }
    }

}
